package com.erp.produce.service.impl;

import com.erp.common.util.ServiceUtil;
import com.erp.common.util.StringUtil;
import com.erp.produce.dao.GoodsDao;
import com.erp.produce.entity.Goods;
import com.erp.produce.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {


    @Autowired
    GoodsDao goodsDao;

    @Override
    public Boolean update(Goods goods) {
        Goods goods1 = goodsDao.findById(goods.getId()).get();
        goods1.setGoodName(goods.getGoodName());
        goods1.setPiecePrize(goods.getPiecePrize());
        goods1.setWeightPrize(goods.getWeightPrize());
        return goodsDao.saveAndFlush(goods1) != null;
}

    @Override
    public Boolean add(Goods goods) {
        return goodsDao.save(goods) != null;
    }




    @Override
    public List<Goods> listPage(int page,int size) {
        List<Goods> goodsList = goodsDao.findAll(PageRequest.of(page, size)).getContent();
        return goodsList;
    }


    @Override
    @Transactional
    public Boolean deleteIds(Long[] ids) {
        return ServiceUtil.deleteIds(ids,goodsDao);
    }

    @Override
    public List<Goods> listPageLike(int page, int size, String name) {
        List<Goods> allByGoodNameLike = goodsDao.findAllByGoodNameLike(StringUtil.addLike(name), PageRequest.of(page, size));
        System.out.println(allByGoodNameLike);
        return allByGoodNameLike;
    }

    @Override
    public List<Goods> list() {
        return goodsDao.findAll(new Sort(Sort.Direction.ASC,"id"));
    }
}